#include "util.hpp"
#include "conf.hpp"
#include "data.hpp"
#include "hot.hpp"
#include "service.hpp"
#include <thread>

void TestFileUtil(const std::string &filename)
{
    // Cloud::FileUtil file(filename);
    // std::cout << file.FileSize() << std::endl;
    // std::cout << file.FileName() << std::endl;
    // std::cout << file.LastModTime() << std::endl;
    // std::cout << file.LastAccTime() << std::endl;

    // Cloud::FileUtil ifile(filename);
    // std::string body;
    // ifile.GetContent(&body);
    // Cloud::FileUtil ofile("./test.cpp");
    // ofile.SetContent(body);

    // Cloud::FileUtil file(filename);
    // file.Compress(filename+".lz");//压缩
    // std::string unpack = "./test.h";
    // file.UnCompress(unpack);//解压缩

    Cloud::FileUtil file(filename);
    file.CreateDirectory();
    std::vector<std::string> array;
    file.ScanDirectory(&array);
    for(auto& s : array)
    {
        std::cout << s << std::endl;
    }
    
}
void TestJsonUtil()
{
    const char *name = "张三";
    int age = 19;
    double score[] = {99.5, 34.5, 89, 45};

    Json::Value root1;
    root1["姓名"] = name;
    root1["年龄"] = age;
    root1["成绩"].append(score[0]);
    root1["成绩"].append(score[1]);
    root1["成绩"].append(score[2]);

    std::string str;
    Cloud::JsonUtil::Serialize(root1, &str);
    std::cout << str << std::endl;
    Json::Value root2;
    Cloud::JsonUtil::UnSerialize(str, &root2);
    std::cout << root2["姓名"].asString() << std::endl;
    std::cout << root2["年龄"].asInt() << std::endl;
    for(int i = 0; i < root2["成绩"].size(); ++i)
    {
        std::cout << root2["成绩"][i].asDouble() << " ";
    }
    std::cout << std::endl;
}
void TestConfig()
{
    Cloud::Config *cfg = Cloud::Config::GetInstance();
    std::cout << cfg->GetHotTime() << std::endl;
    std::cout << cfg->GetServerIp() << std::endl;
    std::cout << cfg->GetServerPort() << std::endl;
    std::cout << cfg->GetDownLoadPrefix() << std::endl;
    std::cout << cfg->GetPackFileSuffix() << std::endl;
    std::cout << cfg->GetPackDir() << std::endl;
    std::cout << cfg->GetBackDir() << std::endl;
    std::cout << cfg->GetBackUpFile() << std::endl;
}

void TestData(const std::string &filename)
{
    // Cloud::BackUpInfo back;
    // back.NewBackUpInfo(filename);
    // Cloud::DataManager data;
    // data.Insert(back);
    // Cloud::BackUpInfo out;
    // data.GetOneByUrl("/download/bundle.h", &out);
    // std::cout << out.pack_flag << std::endl;
    // std::cout << out.fsize << std::endl;
    // std::cout << out.atime << std::endl;
    // std::cout << out.mtime << std::endl;
    // std::cout << out.real_path << std::endl;
    // std::cout << out.pack_path << std::endl;
    // std::cout << out.url << std::endl;

    // back.pack_flag = 1;
    // data.UpData(back);
    // std::vector<Cloud::BackUpInfo> array;
    // data.GetAll(&array);
    // for(auto &a : array)
    // {
    //     std::cout << a.pack_flag << std::endl;
    //     std::cout << a.fsize << std::endl;
    //     std::cout << a.atime << std::endl;
    //     std::cout << a.mtime << std::endl;
    //     std::cout << a.real_path << std::endl;
    //     std::cout << a.pack_path << std::endl;
    //     std::cout << a.url << std::endl;
    // }

    // data.GetOneByRealPath(filename, &out);
    // std::cout << out.pack_flag << std::endl;
    // std::cout << out.fsize << std::endl;
    // std::cout << out.atime << std::endl;
    // std::cout << out.mtime << std::endl;
    // std::cout << out.real_path << std::endl;
    // std::cout << out.pack_path << std::endl;
    // std::cout << out.url << std::endl;

    Cloud::DataManager data;
    std::vector<Cloud::BackUpInfo> array;
    data.GetAll(&array);
    for(auto &a : array)
    {
        std::cout << a.pack_flag << std::endl;
        std::cout << a.fsize << std::endl;
        std::cout << a.atime << std::endl;
        std::cout << a.mtime << std::endl;
        std::cout << a.real_path << std::endl;
        std::cout << a.pack_path << std::endl;
        std::cout << a.url << std::endl;
    }
}

Cloud::DataManager *_data;
void TestHot()
{
    Cloud::HotManager hot;
    hot.RunModule();
}

void TestService()
{
    Cloud::Service src;
    bool ret = src.RunModule();
}
int main(int argc, char* argv[])
{
    _data = new Cloud::DataManager();//创建一个管理类
    //TestFileUtil(argv[1]);
    //TestJsonUtil();
    //TestConfig();
    //TestData(argv[1]);
    // TestHot();
    // TestService();

    std::thread hot_thread(TestHot);
    std::thread server_thread(TestService);
    hot_thread.join();
    server_thread.join();

    return 0;
}
